home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / Direct3D / ScatterGraph / ScatterGraph.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-10-08  |  32.0 KB  |  915 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Begin VB.Form GraphForm 
  4.    Caption         =   "Data Analysis Scatter Graph"
  5.    ClientHeight    =   6420
  6.    ClientLeft      =   60
  7.    ClientTop       =   345
  8.    ClientWidth     =   7875
  9.    BeginProperty Font 
  10.       Name            =   "MS Sans Serif"
  11.       Size            =   12
  12.       Charset         =   0
  13.       Weight          =   400
  14.       Underline       =   0   'False
  15.       Italic          =   0   'False
  16.       Strikethrough   =   0   'False
  17.    EndProperty
  18.    Icon            =   "ScatterGraph.frx":0000
  19.    LinkTopic       =   "Form1"
  20.    ScaleHeight     =   428
  21.    ScaleMode       =   3  'Pixel
  22.    ScaleWidth      =   525
  23.    StartUpPosition =   3  'Windows Default
  24.    Begin VB.CommandButton Command1 
  25.       Caption         =   "Command1"
  26.       BeginProperty Font 
  27.          Name            =   "MS Sans Serif"
  28.          Size            =   18
  29.          Charset         =   0
  30.          Weight          =   700
  31.          Underline       =   0   'False
  32.          Italic          =   0   'False
  33.          Strikethrough   =   0   'False
  34.       EndProperty
  35.       Height          =   435
  36.       Left            =   1920
  37.       TabIndex        =   0
  38.       Top             =   5820
  39.       Visible         =   0   'False
  40.       Width           =   495
  41.    End
  42.    Begin MSComDlg.CommonDialog CommonDialog1 
  43.       Left            =   1080
  44.       Top             =   5760
  45.       _ExtentX        =   847
  46.       _ExtentY        =   847
  47.       _Version        =   393216
  48.    End
  49.    Begin VB.Timer Timer1 
  50.       Enabled         =   0   'False
  51.       Interval        =   10
  52.       Left            =   240
  53.       Top             =   5760
  54.    End
  55.    Begin VB.Menu MENU_POPUP 
  56.       Caption         =   "POPUPMENU"
  57.       Visible         =   0   'False
  58.       Begin VB.Menu MENU_EXITMENU 
  59.          Caption         =   "Exit Menu!"
  60.       End
  61.       Begin VB.Menu MENU_LOAD 
  62.          Caption         =   "Load Data From File!"
  63.       End
  64.       Begin VB.Menu MENU_RESET 
  65.          Caption         =   "Reset Orientation!"
  66.       End
  67.       Begin VB.Menu MENU_CONNECT 
  68.          Caption         =   "Show connecting lines"
  69.          Checked         =   -1  'True
  70.       End
  71.       Begin VB.Menu MENU_LINES 
  72.          Caption         =   "Show height lines"
  73.          Checked         =   -1  'True
  74.       End
  75.       Begin VB.Menu MENU_FOOTLINES 
  76.          Caption         =   "Show foot lines"
  77.          Checked         =   -1  'True
  78.       End
  79.       Begin VB.Menu MENU_BASE 
  80.          Caption         =   "Show base plane"
  81.          Checked         =   -1  'True
  82.       End
  83.       Begin VB.Menu MENU_ROTATE 
  84.          Caption         =   "Auto Rotate"
  85.          Checked         =   -1  'True
  86.       End
  87.    End
  88. Attribute VB_Name = "GraphForm"
  89. Attribute VB_GlobalNameSpace = False
  90. Attribute VB_Creatable = False
  91. Attribute VB_PredeclaredId = True
  92. Attribute VB_Exposed = False
  93. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  94. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  95. '  File:       ScatterGraph.frm
  96. '  Content:    Implementation of a plot graph in 3 dimensions
  97. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  98. Option Explicit
  99. Dim m_maxX As Double
  100. Dim m_minX As Double
  101. Dim m_maxY As Double
  102. Dim m_minY As Double
  103. Dim m_maxZ As Double
  104. Dim m_minZ As Double
  105. Dim m_maxsize As Double
  106. Dim m_minSize As Double
  107. Dim m_extX As Double
  108. Dim m_extY As Double
  109. Dim m_extZ As Double
  110. Dim m_extSize As Double
  111. Dim m_scalex As Single
  112. Dim m_scaley As Single
  113. Dim m_scalez As Single
  114. Dim m_scalesize As Single
  115. Dim m_xHeader As String
  116. Dim m_yHeader As String
  117. Dim m_zHeader As String
  118. Dim m_sizeHeader As String
  119. Dim m_binit As Boolean
  120. Dim m_bGraphInit As Boolean
  121. Dim m_bMinimized As Boolean
  122. Dim m_graphroot As CD3DFrame
  123. Dim m_quad1 As CD3DFrame
  124. Dim m_quad2 As CD3DFrame
  125. Dim m_XZPlaneFrame As CD3DFrame
  126. Dim m_bRot As Boolean
  127. Dim m_bHeightLines As Boolean
  128. Dim m_bConnectlines As Boolean
  129. Dim m_bShowBase As Boolean
  130. Dim m_bFootLines As Boolean
  131. Dim m_drawtext As String
  132. Dim m_drawtextpos As RECT
  133. Dim m_drawtextEnable As Boolean
  134. Dim m_formatX As String
  135. Dim m_formatY As String
  136. Dim m_formatZ As String
  137. Dim m_formatSize As String
  138. Dim m_data As Collection
  139. Dim m_hwnd As Long
  140. Dim m_vbfont As IFont
  141. Dim m_vbfont2 As IFont
  142. Dim m_font2height  As Long
  143. Dim m_lastX As Single
  144. Dim m_lasty As Single
  145. Dim m_bMouseDown As Boolean
  146. Dim m_Tex As Direct3DTexture8
  147. Dim m_LabelX As CD3DFrame
  148. Dim m_LabelY As CD3DFrame
  149. Dim m_LabelZ As CD3DFrame
  150. Dim m_meshobj As D3DXMesh
  151. Dim m_meshplane As D3DXMesh
  152. Dim m_font As D3DXFont
  153. Dim m_font2 As D3DXFont
  154. 'Camera variables
  155. Dim m_fElapsedTime As Single
  156. Dim m_vVelocity  As D3DVECTOR
  157. Dim m_fYawVelocity As Single
  158. Dim m_fPitchVelocity As Single
  159. Dim m_fYaw As Single
  160. Dim m_fPitch As Single
  161. Dim m_vPosition As D3DVECTOR
  162. Dim m_bKey(256) As Boolean
  163. Dim m_matView As D3DMATRIX
  164. Dim m_matOrientation As D3DMATRIX
  165. Dim m_MediaDir As String
  166. Const kdx = 256&
  167. Const kdy = 256&
  168. Const D3DFVF_VERTEX = D3DFVF_XYZ Or D3DFVF_NORMAL Or D3DFVF_TEX1
  169. Friend Sub Init(hwnd As Long, font As IFontDisp, font2 As IFontDisp)
  170.     Dim i As Long
  171.     'Save hwnd
  172.     m_hwnd = hwnd
  173.     'convert IFontDisp to Ifont
  174.     Set m_vbfont = font
  175.     Set m_vbfont2 = font2
  176.     'initialized d3d
  177.     m_binit = D3DUtil_Init(hwnd, True, 0, 0, D3DDEVTYPE_HAL, Nothing)
  178.         
  179.     'exit if initialization failed
  180.     If m_binit = False Then End
  181.     DeleteDeviceObjects
  182.     InitDeviceObjects
  183.     BuildDefaultDataList
  184.     ComputeDataExtents
  185.     BuildGraph
  186.     RestoreDeviceObjects
  187.     DoEvents
  188.     m_bRot = True
  189.     m_xHeader = "X Axis"
  190.     m_yHeader = "Y Axis"
  191.     m_zHeader = "Z Axis"
  192.     m_sizeHeader = "s"
  193.     m_vPosition = vec3(0, 0, -20)
  194.     'Initialze camera matrices
  195.     g_dev.GetTransform D3DTS_VIEW, m_matView
  196.     D3DXMatrixTranslation m_matOrientation, 0, 0, 0
  197.     Timer1.Enabled = True
  198.     Call DXUtil_Timer(TIMER_start)
  199. End Sub
  200. Private Sub BuildDefaultDataList()
  201.     Set m_data = New Collection
  202.     Dim i As Single
  203.     For i = 1 To 40 Step 2
  204.         AddEntry "pt" + CStr(i), 1 / CSng(i), (i * i) - 25 * i, CSng(i), (0.7 + i / 16), D3DCOLORVALUEtoLONG(ColorValue4(1, 1, 0.5 + i / 20, i / 80)), ""
  205.     Next
  206.     m_formatX = "0.000"
  207.     m_formatY = "0.000"
  208.     m_formatZ = "0.000"
  209.     m_formatSize = "0.000"
  210.     m_bConnectlines = True
  211.     m_bHeightLines = True
  212.     m_bShowBase = True
  213.     m_bFootLines = True
  214.     m_xHeader = "X Axis"
  215.     m_yHeader = "Y Axis"
  216.     m_zHeader = "Z Axis"
  217.     m_sizeHeader = "s"
  218. End Sub
  219. Sub RestoreDeviceObjects()
  220.     g_lWindowWidth = Me.ScaleWidth
  221.     g_lWindowHeight = Me.ScaleHeight
  222.     D3DUtil_SetupDefaultScene
  223.     D3DUtil_SetupCamera vec3(0, 5, -20), vec3(0, 0, 0), vec3(0, 1, 0)
  224.     'allow the application to show both sides of all surfaces
  225.     g_dev.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE
  226.     'turn on min filtering since our text is often smaller
  227.     'than original size
  228.     g_dev.SetTextureStageState 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR
  229.      Set m_font = g_d3dx.CreateFont(g_dev, m_vbfont.hFont)
  230.     Set m_font2 = g_d3dx.CreateFont(g_dev, m_vbfont2.hFont)
  231.         
  232. End Sub
  233. Private Sub ComputeDataExtents()
  234.     Dim mind As Single
  235.     Dim maxd As Single
  236.     Dim entry As DataEntry
  237.     mind = -9E+20
  238.     maxd = 9E+20
  239.     m_maxX = mind:    m_maxY = mind:    m_maxZ = mind:    m_maxsize = mind
  240.     m_minX = maxd:    m_minY = maxd:    m_minZ = maxd:    m_minSize = maxd
  241.     'Dim entry As DataEntry
  242.     For Each entry In m_data
  243.                         
  244.         If entry.datax > m_maxX Then m_maxX = entry.datax
  245.         If entry.datay > m_maxY Then m_maxY = entry.datay
  246.         If entry.dataz > m_maxZ Then m_maxZ = entry.dataz
  247.         If entry.dataSize > m_maxsize Then m_maxsize = entry.dataSize
  248.         
  249.         If entry.datax < m_minX Then m_minX = entry.datax
  250.         If entry.datay < m_minY Then m_minY = entry.datay
  251.         If entry.dataz < m_minZ Then m_minZ = entry.dataz
  252.         If entry.dataSize < m_minSize Then m_minSize = entry.dataSize
  253.                 
  254.     Next
  255.     m_extX = m_maxX - m_minX
  256.     m_extY = m_maxY - m_minY
  257.     m_extZ = m_maxZ - m_minZ
  258.     m_extSize = m_maxsize - m_minSize
  259.     Dim kScale As Single
  260.     kScale = 5
  261.     m_scalex = 1
  262.     m_scaley = 1
  263.     m_scalez = 1
  264.     m_scalesize = 1
  265.     If m_maxX > Abs(m_minX) Then
  266.         If m_maxX <> 0 Then m_scalex = kScale / m_maxX
  267.     Else
  268.         If m_minX <> 0 Then m_scalex = kScale / Abs(m_minX)
  269.     End If
  270.     If m_maxY > Abs(m_minY) Then
  271.         If m_maxY <> 0 Then m_scaley = kScale / m_maxY
  272.     Else
  273.         If m_minY <> 0 Then m_scaley = kScale / Abs(m_minY)
  274.     End If
  275.     If m_maxZ > Abs(m_minZ) Then
  276.         If m_maxZ <> 0 Then m_scalez = kScale / m_maxZ
  277.     Else
  278.         If m_minZ <> 0 Then m_scalez = kScale / Abs(m_minZ)
  279.     End If
  280.     If m_maxsize = 0 Then m_maxsize = 1
  281.     m_scalesize = 1 * (kScale) / m_maxsize
  282.         
  283.     'scale graph data to fit
  284.     For Each entry In m_data
  285.                      
  286.         entry.x = entry.datax * m_scalex
  287.         entry.y = entry.datay * m_scaley
  288.         entry.z = entry.dataz * m_scalez
  289.         entry.size = entry.dataSize * m_scalesize
  290.     Next
  291. End Sub
  292. Public Sub AddEntry(sName As String, x As Double, y As Double, z As Double, size As Double, color As Long, data As Variant)
  293.     On Local Error GoTo errOut
  294.     Dim entry As New DataEntry
  295.     entry.dataname = sName
  296.     entry.datax = x
  297.     entry.datay = y
  298.     entry.dataz = z
  299.     entry.dataSize = size
  300.     entry.color = color
  301.     entry.data = data
  302.     m_data.Add entry
  303.     Exit Sub
  304. errOut:
  305.     MsgBox "unable to add entry"
  306. End Sub
  307. Public Sub DrawGraph()
  308.     Dim entry As DataEntry
  309.     Dim hr As Long
  310.     If m_binit = False Then Exit Sub
  311.     'See what state the device is in.
  312.     hr = g_dev.TestCooperativeLevel
  313.     If hr = D3DERR_DEVICENOTRESET Then
  314.         g_dev.Reset g_d3dpp
  315.         RestoreDeviceObjects
  316.     ElseIf hr <> 0 Then
  317.         Exit Sub
  318.     End If
  319.     m_graphroot.UpdateFrames
  320.              
  321.     'Clear the previous render with the backgroud color
  322.     'We clear to grey but notice that we are using a hexidecimal
  323.     'number to represent Alpha Red Green and blue
  324.     D3DUtil_ClearAll &HFF707070
  325.     'set the ambient lighting level
  326.     g_dev.SetRenderState D3DRS_AMBIENT, &HFFC0C0C0
  327.     g_dev.BeginScene
  328.         
  329.         
  330.     'only render objects underneath the xzplane
  331.     m_quad1.Enabled = False
  332.     m_quad2.Enabled = True
  333.     m_XZPlaneFrame.Enabled = False
  334.     m_graphroot.Render g_dev
  335.     'render the objects in front of xz plane
  336.     m_quad1.Enabled = True
  337.     m_quad2.Enabled = False
  338.     m_XZPlaneFrame.Enabled = False
  339.     m_graphroot.Render g_dev
  340.         
  341.         
  342.         
  343.     DrawLines 0
  344.     DrawAxisNameSquare 0    'x axis
  345.     DrawAxisNameSquare 2    'z axis
  346.         
  347.     'draw pop up text
  348.     If m_drawtextEnable Then
  349.         g_d3dx.DrawText m_font, &HFF00FFFF, m_drawtext, m_drawtextpos, 0
  350.     End If
  351.     Dim rc As RECT
  352.     rc.Top = 20:    rc.Left = 10
  353.     g_d3dx.DrawText m_font, &HFF00FFFF, "Height = " + m_yHeader, rc, 0
  354.     rc.Top = 40:    rc.Left = 10
  355.     g_d3dx.DrawText m_font, &HFF00FFFF, "Size = " + m_sizeHeader, rc, 0
  356.     'render the xzplane with transparency
  357.     If m_bShowBase Then
  358.         m_quad1.Enabled = False
  359.         m_quad2.Enabled = False
  360.         m_XZPlaneFrame.Enabled = True
  361.         m_graphroot.Render g_dev
  362.     End If
  363.     g_dev.EndScene
  364.     D3DUtil_PresentAll m_hwnd
  365. End Sub
  366. Public Sub BuildGraph()
  367.     Dim entry As DataEntry
  368.     Dim material As D3DMATERIAL8
  369.     Dim newFrame As CD3DFrame
  370.     Dim i As Long
  371.     Dim d3ddm As D3DDISPLAYMODE
  372.         
  373.     If m_binit = False Then Exit Sub
  374.     'Create rotatable root object
  375.     Set m_graphroot = D3DUtil_CreateFrame(Nothing)
  376.                 
  377.     'Create XZ plane for reference
  378.     material.diffuse = LONGtoD3DCOLORVALUE(&H6FC0C0C0)
  379.     material.Ambient = material.diffuse
  380.     Set m_XZPlaneFrame = D3DUtil_CreateFrame(m_graphroot)
  381.     m_XZPlaneFrame.AddD3DXMesh(m_meshplane).SetMaterialOverride material
  382.     m_XZPlaneFrame.SetOrientation D3DUtil_RotationAxis(1, 0, 0, 90)
  383.     Set m_quad1 = D3DUtil_CreateFrame(m_graphroot)
  384.     Set m_quad2 = D3DUtil_CreateFrame(m_graphroot)
  385.     Set m_LabelX = D3DUtil_CreateFrame(m_graphroot)
  386.     m_LabelX.SetPosition vec3(0, 0, -6)
  387.     Set m_LabelY = D3DUtil_CreateFrame(Nothing)
  388.     m_LabelY.SetPosition vec3(-8, 8, 0)
  389.     Set m_LabelZ = D3DUtil_CreateFrame(m_graphroot)
  390.     m_LabelZ.SetPosition vec3(6, 0, 0)
  391.     m_LabelZ.SetOrientation D3DUtil_RotationAxis(0, 1, 0, -90)
  392.     Dim quadframe As CD3DFrame
  393.     For Each entry In m_data
  394.         If entry.y >= 0 Then Set quadframe = m_quad1
  395.         If entry.y < 0 Then Set quadframe = m_quad2
  396.                 
  397.         'Set material of objects
  398.         material.diffuse = LONGtoD3DCOLORVALUE(entry.color)
  399.         material.Ambient = material.diffuse
  400.                 
  401.         'Create individual objects
  402.         Set newFrame = D3DUtil_CreateFrame(quadframe)
  403.         newFrame.SetScale entry.size
  404.         newFrame.SetPosition vec3(entry.x, entry.y, entry.z)
  405.         newFrame.AddD3DXMesh(m_meshobj).SetMaterialOverride material
  406.         i = i + 1
  407.         newFrame.ObjectName = Str(i)
  408.    Next
  409.    'Take care of labels
  410.     Dim surf As Direct3DSurface8
  411.     Dim rc As RECT
  412.     Dim rts As D3DXRenderToSurface
  413.     Dim rtsviewport As D3DVIEWPORT8
  414.     Set surf = m_Tex.GetSurfaceLevel(0)
  415.     rtsviewport.height = kdx
  416.     rtsviewport.width = kdy
  417.     rtsviewport.MaxZ = 1
  418.     Call g_dev.GetDisplayMode(d3ddm)
  419.     Set rts = g_d3dx.CreateRenderToSurface(g_dev, kdx, kdy, d3ddm.format, 1, D3DFMT_D16)
  420.     rts.BeginScene surf, rtsviewport
  421.     g_dev.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, &HFFC0C0C0, 1, 0
  422.         
  423.     g_d3dx.DrawText m_font2, &HFF000000, m_xHeader, rc, DT_CALCRECT
  424.     m_font2height = rc.bottom
  425.     rc.Top = m_font2height * 0: rc.Left = 10: rc.bottom = 0: rc.Right = 0
  426.     g_d3dx.DrawText m_font2, &HFF000000, m_xHeader, rc, DT_CALCRECT
  427.     g_d3dx.DrawText m_font2, &HFF000000, m_xHeader, rc, 0
  428.     rc.Top = m_font2height * 1: rc.Left = 10: rc.bottom = 0: rc.Right = 0
  429.     g_d3dx.DrawText m_font2, &HFF000000, m_yHeader, rc, DT_CALCRECT
  430.     g_d3dx.DrawText m_font2, &HFF000000, m_yHeader, rc, 0
  431.     rc.Top = m_font2height * 2: rc.Left = 10: rc.bottom = 0: rc.Right = 0
  432.     g_d3dx.DrawText m_font2, &HFF000000, m_zHeader, rc, DT_CALCRECT
  433.     g_d3dx.DrawText m_font2, &HFF000000, m_zHeader, rc, 0
  434.     rts.EndScene
  435.    m_bGraphInit = True
  436. End Sub
  437. Public Sub InitDeviceObjects()
  438.     Dim d3ddm As D3DDISPLAYMODE
  439.     If m_binit = False Then Exit Sub
  440.     Dim rc As RECT
  441.     Set m_meshobj = g_d3dx.CreateSphere(g_dev, 0.1, 16, 16, Nothing)
  442.     Set m_meshplane = g_d3dx.CreateBox(g_dev, 10, 10, 0.1, Nothing)
  443.     Set m_font = g_d3dx.CreateFont(g_dev, m_vbfont.hFont)
  444.     Set m_font2 = g_d3dx.CreateFont(g_dev, m_vbfont2.hFont)
  445.     Call g_dev.GetDisplayMode(d3ddm)
  446.     'Create Textures
  447.     Set m_Tex = g_d3dx.CreateTexture(g_dev, kdx, kdx, 0, 0, d3ddm.format, D3DPOOL_MANAGED)
  448.        
  449. End Sub
  450. Private Sub DrawLines(quad As Long)
  451.     Dim entry As DataEntry
  452.     Dim vLast As D3DVECTOR, vNext As D3DVECTOR
  453.     Dim vGround As D3DVECTOR
  454.     Dim vGround1 As D3DVECTOR
  455.     Dim vGround2 As D3DVECTOR
  456.     Dim i As Long
  457.     'Link lines
  458.     g_dev.SetTransform D3DTS_WORLD, m_graphroot.GetMatrix
  459.     Set entry = m_data.item(1)
  460.     vLast = vec3(entry.x, entry.y, entry.z)
  461.     vGround = vLast
  462.     vGround.y = 0
  463.     Call DrawLine(vGround, vLast, &HFFFF0000)
  464.     For i = 2 To m_data.count
  465.         Set entry = m_data.item(i)
  466.         vNext = vec3(entry.x, entry.y, entry.z)
  467.         
  468.         If m_bConnectlines Then
  469.             Call DrawLine(vLast, vNext, &HFFFF00FF)
  470.         End If
  471.         
  472.         vGround = vNext
  473.         vGround.y = 0
  474.         vGround1 = vGround
  475.         vGround1.y = 0.1
  476.         vGround2 = vLast
  477.         vGround2.y = 0.1
  478.         
  479.         If m_bHeightLines Then
  480.             Call DrawLine(vGround, vNext, &HFFFF0000)
  481.         End If
  482.         
  483.         If m_bFootLines Then
  484.             Call DrawLine(vGround1, vGround2, &HFF10FF30)
  485.         End If
  486.         
  487.         vLast = vNext
  488.     Next
  489.     DrawLine vec3(-5, 0.1, 0), vec3(5, 0.1, 0), &HFF0&
  490.     DrawLine vec3(0, 0.1, -5), vec3(0, 0.1, 5), &HFF0&
  491. End Sub
  492. Private Sub DrawLine(v1 As D3DVECTOR, v2 As D3DVECTOR, color As Long)
  493.     Dim mat As D3DMATERIAL8
  494.     mat.diffuse = LONGtoD3DCOLORVALUE(color)
  495.     mat.Ambient = mat.diffuse
  496.     g_dev.SetMaterial mat
  497.     Dim dataOut(2) As D3DVERTEX
  498.     LSet dataOut(0) = v1
  499.     LSet dataOut(1) = v2
  500.     g_dev.SetVertexShader D3DFVF_VERTEX
  501.     g_dev.DrawPrimitiveUP D3DPT_LINELIST, 1, dataOut(0), Len(dataOut(0))
  502. End Sub
  503. Public Sub MouseOver(Button As Integer, Shift As Integer, x As Single, y As Single)
  504.     If m_binit = False Then Exit Sub
  505.     Dim pick As New CD3DPick
  506.     Dim frame As CD3DFrame
  507.     Dim nid As Long
  508.     Dim entry As DataEntry
  509.     'remove the XZ plane from consideration for pick
  510.     m_XZPlaneFrame.Enabled = False
  511.     m_quad1.Enabled = True
  512.     m_quad2.Enabled = True
  513.     pick.ViewportPick m_graphroot, x, y
  514.     nid = pick.FindNearest()
  515.     If nid < 0 Then
  516.         m_drawtextEnable = False
  517.         Exit Sub
  518.     End If
  519.         
  520.     Set frame = pick.GetFrame(nid)
  521.     'have matrices pre computed for scene graph
  522.     m_graphroot.UpdateFrames
  523.     'due some math to get position of item in screen space
  524.     Dim viewport As D3DVIEWPORT8
  525.     Dim projmatrix As D3DMATRIX
  526.     Dim viewmatrix As D3DMATRIX
  527.     Dim vOut As D3DVECTOR
  528.     g_dev.GetViewport viewport
  529.     g_dev.GetTransform D3DTS_PROJECTION, projmatrix
  530.     g_dev.GetTransform D3DTS_VIEW, viewmatrix
  531.     D3DXVec3Project vOut, vec3(0, 0, 0), viewport, projmatrix, viewmatrix, frame.GetUpdatedMatrix
  532.             
  533.     Debug.Print vOut.x, vOut.y, frame.ObjectName
  534.     Dim destRect As RECT
  535.     m_drawtextpos.Left = x - 20
  536.     m_drawtextpos.Top = y - 70
  537.     If m_drawtextpos.Left < 0 Then m_drawtextpos.Left = 1
  538.     If m_drawtextpos.Top < 0 Then m_drawtextpos.Top = 1
  539.     Set entry = m_data.item(val(frame.ObjectName))
  540.     With entry
  541.         m_drawtext = .dataname + Chr(13)
  542.         m_drawtext = m_drawtext + " " + m_xHeader + "=" + format$(.datax, m_formatX) + Chr(13)
  543.         m_drawtext = m_drawtext + " " + m_yHeader + "=" + format$(.datay, m_formatY) + Chr(13)
  544.         m_drawtext = m_drawtext + " " + m_zHeader + "=" + format$(.dataz, m_formatZ) + Chr(13)
  545.         m_drawtext = m_drawtext + " " + m_sizeHeader + "=" + format$(.dataSize, m_formatSize)
  546.     End With
  547.     m_drawtextEnable = True
  548. End Sub
  549. Sub FrameMove()
  550.     'for camera movement
  551.     m_fElapsedTime = DXUtil_Timer(TIMER_GETELLAPSEDTIME) * 1.3
  552.     If m_fElapsedTime < 0 Then Exit Sub
  553.         
  554.         
  555.     If m_bRot And m_bMouseDown = False Then
  556.         m_graphroot.AddRotation COMBINE_BEFORE, 0, 1, 0, (g_pi / 40) * m_fElapsedTime
  557.     End If
  558.         
  559.         
  560.     ' Slow things down for the REF device
  561.     If (g_devType = D3DDEVTYPE_REF) Then m_fElapsedTime = 0.05
  562.     Dim fSpeed As Single
  563.     Dim fAngularSpeed
  564.     fSpeed = 5 * m_fElapsedTime
  565.     fAngularSpeed = 1 * m_fElapsedTime
  566.     ' Slowdown the camera movement
  567.     D3DXVec3Scale m_vVelocity, m_vVelocity, 0.9
  568.     m_fYawVelocity = m_fYawVelocity * 0.9
  569.     m_fPitchVelocity = m_fPitchVelocity * 0.9
  570.     ' Process keyboard input
  571.     If (m_bKey(vbKeyRight)) Then m_vVelocity.x = m_vVelocity.x + fSpeed        '  Slide Right
  572.     If (m_bKey(vbKeyLeft)) Then m_vVelocity.x = m_vVelocity.x - fSpeed         '  Slide Left
  573.     If (m_bKey(vbKeyUp)) Then m_vVelocity.y = m_vVelocity.y + fSpeed           '  Move up
  574.     If (m_bKey(vbKeyDown)) Then m_vVelocity.y = m_vVelocity.y - fSpeed         '  Move down
  575.     If (m_bKey(vbKeyW)) Then m_vVelocity.z = m_vVelocity.z + fSpeed            '  Move Forward
  576.     If (m_bKey(vbKeyS)) Then m_vVelocity.z = m_vVelocity.z - fSpeed            '  Move Backward
  577.     If (m_bKey(vbKeyE)) Then m_fYawVelocity = m_fYawVelocity + fSpeed          '  Yaw right
  578.     If (m_bKey(vbKeyQ)) Then m_fYawVelocity = m_fYawVelocity - fSpeed          '  Yaw left
  579.     If (m_bKey(vbKeyZ)) Then m_fPitchVelocity = m_fPitchVelocity + fSpeed      '  turn down
  580.     If (m_bKey(vbKeyA)) Then m_fPitchVelocity = m_fPitchVelocity - fSpeed      '  turn up
  581.     ' Update the position vector
  582.     Dim vT As D3DVECTOR, vTemp As D3DVECTOR
  583.     D3DXVec3Scale vTemp, m_vVelocity, fSpeed
  584.     D3DXVec3Add vT, vT, vTemp
  585.     D3DXVec3TransformNormal vT, vT, m_matOrientation
  586.     D3DXVec3Add m_vPosition, m_vPosition, vT
  587.     If (m_vPosition.y < 1) Then m_vPosition.y = 1
  588.     ' Update the yaw-pitch-rotation vector
  589.     m_fYaw = m_fYaw + fAngularSpeed * m_fYawVelocity
  590.     m_fPitch = m_fPitch + fAngularSpeed * m_fPitchVelocity
  591.     If (m_fPitch < 0) Then m_fPitch = 0
  592.     If (m_fPitch > g_pi / 2) Then m_fPitch = g_pi / 2
  593.     Dim qR As D3DQUATERNION, det As Single
  594.     D3DXQuaternionRotationYawPitchRoll qR, m_fYaw, m_fPitch, 0
  595.     D3DXMatrixAffineTransformation m_matOrientation, 1.25, vec3(0, 0, 0), qR, m_vPosition
  596.     D3DXMatrixInverse m_matView, det, m_matOrientation
  597.         'set new view matrix
  598.     g_dev.SetTransform D3DTS_VIEW, m_matView
  599. End Sub
  600. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  601.     m_bKey(KeyCode) = True
  602. End Sub
  603. Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
  604.     m_bKey(KeyCode) = False
  605. End Sub
  606. Private Sub Form_Load()
  607.     'Show the form
  608.     Me.Show
  609.     DoEvents
  610.         
  611.     m_MediaDir = FindMediaDir("ScatterData.csv")
  612.     D3DUtil.D3DUtil_SetMediaPath m_MediaDir
  613.     'initialize the graph
  614.     Init Me.hwnd, Me.font, Command1.font
  615. End Sub
  616. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  617.     If Button = 2 Then
  618.         Me.PopupMenu MENU_POPUP
  619.     Else
  620.         '- save our current position
  621.         m_bMouseDown = True
  622.         m_lastX = x
  623.         m_lasty = y
  624.         
  625.     End If
  626. End Sub
  627. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  628.         
  629.     If m_binit = False Then Exit Sub
  630.     If Button = 2 Then Exit Sub
  631.     If m_bMouseDown = False Then
  632.         Call MouseOver(Button, Shift, x, y)
  633.     Else
  634.         '- Rotate the object
  635.         RotateTrackBall CInt(x), CInt(y)
  636.     End If
  637.     FrameMove
  638.     DrawGraph
  639. End Sub
  640. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  641.     m_bMouseDown = False
  642. End Sub
  643. '-----------------------------------------------------------------------------
  644. ' Name: Form_Resize()
  645. ' Desc: hadle resizing of the D3D backbuffer
  646. '-----------------------------------------------------------------------------
  647. Private Sub Form_Resize()
  648.     Timer1.Enabled = False
  649.     ' If D3D is not initialized then exit
  650.     If Not m_binit Then Exit Sub
  651.     ' If we are in a minimized state stop the timer and exit
  652.     If Me.WindowState = vbMinimized Then
  653.         DXUtil_Timer TIMER_STOP
  654.         m_bMinimized = True
  655.         Exit Sub
  656.         
  657.     ' If we just went from a minimized state to maximized
  658.     ' restart the timer
  659.     Else
  660.         If m_bMinimized = True Then
  661.             DXUtil_Timer TIMER_start
  662.             m_bMinimized = False
  663.         End If
  664.     End If
  665.     ' Dont let the window get too small
  666.     If Me.ScaleWidth < 10 Then
  667.         Me.width = Screen.TwipsPerPixelX * 10
  668.         Exit Sub
  669.     End If
  670.     If Me.ScaleHeight < 10 Then
  671.         Me.height = Screen.TwipsPerPixelY * 10
  672.         Exit Sub
  673.     End If
  674.      
  675.     DeleteDeviceObjects
  676.     'reset and resize our D3D backbuffer to the size of the window
  677.     D3DUtil_ResizeWindowed Me.hwnd
  678.     'All state get losts after a reset so we need to reinitialze it here
  679.     RestoreDeviceObjects
  680.     Timer1.Enabled = True
  681. End Sub
  682. '- Rotate Track ball
  683. '  given a point on the screen the mouse was moved to
  684. '  simulate a track ball
  685. Private Sub RotateTrackBall(x As Integer, y As Integer)
  686.     Dim delta_x As Single, delta_y As Single
  687.     Dim delta_r As Single, radius As Single, denom As Single, angle As Single
  688.     ' rotation axis in camcoords, worldcoords, sframecoords
  689.     Dim axisC As D3DVECTOR
  690.     Dim wc As D3DVECTOR
  691.     Dim axisS As D3DVECTOR
  692.     Dim base As D3DVECTOR
  693.     Dim origin As D3DVECTOR
  694.     delta_x = x - m_lastX
  695.     delta_y = y - m_lasty
  696.     m_lastX = x
  697.     m_lasty = y
  698.             
  699.      delta_r = Sqr(delta_x * delta_x + delta_y * delta_y)
  700.      radius = 50
  701.      denom = Sqr(radius * radius + delta_r * delta_r)
  702.     If (delta_r = 0 Or denom = 0) Then Exit Sub
  703.     angle = (delta_r / denom)
  704.     axisC.x = (-delta_y / delta_r)
  705.     axisC.y = (-delta_x / delta_r)
  706.     axisC.z = 0
  707.     'transform camera space vector to world space
  708.     'm_largewindow.m_cameraFrame.Transform wc, axisC
  709.     g_dev.GetTransform D3DTS_VIEW, g_viewMatrix
  710.     D3DXVec3TransformCoord wc, axisC, g_viewMatrix
  711.     'transform world space vector into Model space
  712.     m_graphroot.UpdateFrames
  713.     axisS = m_graphroot.InverseTransformCoord(wc)
  714.         
  715.     'transform origen camera space to world coordinates
  716.     'm_largewindow.m_cameraFrame.Transform  wc, origin
  717.     D3DXVec3TransformCoord wc, origin, g_viewMatrix
  718.     'transfer cam space origen to model space
  719.     base = m_graphroot.InverseTransformCoord(wc)
  720.     axisS.x = axisS.x - base.x
  721.     axisS.y = axisS.y - base.y
  722.     axisS.z = axisS.z - base.z
  723.     m_graphroot.AddRotation COMBINE_BEFORE, axisS.x, axisS.y, axisS.z, angle
  724. End Sub
  725. Private Sub Form_Paint()
  726.     If Not m_binit Then Exit Sub
  727.     If Not m_bGraphInit Then Exit Sub
  728.     DrawGraph
  729. End Sub
  730. Private Sub Form_Unload(Cancel As Integer)
  731.     End
  732. End Sub
  733. Private Sub MENU_BASE_Click()
  734.     m_bShowBase = Not m_bShowBase
  735.     MENU_BASE.Checked = m_bShowBase
  736. End Sub
  737. Private Sub MENU_CONNECT_Click()
  738.     m_bConnectlines = Not m_bConnectlines
  739.     MENU_CONNECT.Checked = m_bConnectlines
  740. End Sub
  741. Private Sub MENU_FOOTLINES_Click()
  742.     m_bFootLines = Not m_bFootLines
  743.     MENU_FOOTLINES.Checked = m_bFootLines
  744. End Sub
  745. Private Sub MENU_LINES_Click()
  746.     m_bHeightLines = Not m_bHeightLines
  747.     MENU_LINES.Checked = m_bHeightLines
  748. End Sub
  749. Private Sub MENU_LOAD_Click()
  750.     Dim sFile As String
  751.     CommonDialog1.FileName = ""
  752.     CommonDialog1.DefaultExt = "csv"
  753.     CommonDialog1.filter = "csv|*.csv"
  754.     CommonDialog1.InitDir = m_MediaDir
  755.     On Local Error Resume Next
  756.     CommonDialog1.ShowOpen
  757.     sFile = CommonDialog1.FileName
  758.     If sFile = "" Then Exit Sub
  759.     LoadFile sFile
  760.     Set m_graphroot = Nothing
  761.     Set m_quad1 = Nothing
  762.     Set m_quad2 = Nothing
  763.     Set m_XZPlaneFrame = Nothing
  764.     ComputeDataExtents
  765.     BuildGraph
  766.     RestoreDeviceObjects
  767. End Sub
  768. Private Sub MENU_RESET_Click()
  769.     m_graphroot.SetMatrix g_identityMatrix
  770.     m_vPosition = vec3(0, 0, -20)
  771.     m_fYaw = 0
  772.     m_fPitch = 0
  773.     Call D3DXMatrixTranslation(m_matOrientation, 0, 0, 0)
  774. End Sub
  775. Private Sub MENU_ROTATE_Click()
  776.     m_bRot = Not m_bRot
  777.     MENU_ROTATE.Checked = m_bRot
  778. End Sub
  779. Private Sub Timer1_Timer()
  780.     If Not m_binit Then Exit Sub
  781.     FrameMove
  782.     DrawGraph
  783. End Sub
  784. Sub LoadFile(sFile As String)
  785.     If Dir$(sFile) = "" Then
  786.         MsgBox "Unable to find " + sFile
  787.         Exit Sub
  788.     End If
  789.     Dim fl As Long
  790.     Dim strIn As String
  791.     Dim strTrim As String
  792.     Dim strFirstChar As String
  793.     Dim splitArray
  794.     Dim cols As Long
  795.     Dim bFoundData As Boolean
  796.     Dim sName As String
  797.     Dim x As Double
  798.     Dim y As Double
  799.     Dim z As Double
  800.     Dim size As Double
  801.     Dim color As Long
  802.     Dim data
  803.     Dim i As Long
  804.     Dim olddata As Collection
  805.     fl = FreeFile
  806.     On Local Error GoTo errOut
  807.     Set olddata = m_data
  808.     Set m_data = New Collection
  809.     Open sFile For Input As fl
  810.         
  811.     Do While Not EOF(fl)
  812.         Line Input #fl, strIn
  813.         strTrim = Trim(strIn)
  814.         
  815.         'skip comment lines
  816.         strFirstChar = Mid$(strTrim, 1, 1)
  817.         If strFirstChar = "#" Or strFirstChar = ";" Then GoTo nextLine
  818.         If strTrim = "" Then GoTo nextLine
  819.         
  820.         splitArray = Split(strTrim, ",")
  821.         
  822.         cols = UBound(splitArray)
  823.         If cols < 4 Then
  824.             MsgBox "Comma delimited file must have at least 4 columns (name,x,y,z)"
  825.             Exit Sub
  826.         End If
  827.                 
  828.         
  829.         'If we have not found numbers see if we found a header row
  830.         If Not bFoundData Then
  831.             If IsNumeric(splitArray(1)) = False Then
  832.                 'assume data is a header row
  833.                 m_xHeader = CStr(splitArray(1))
  834.                 m_yHeader = CStr(splitArray(2))
  835.                 m_zHeader = CStr(splitArray(3))
  836.                 m_sizeHeader = CStr(splitArray(4))
  837.                 GoTo nextLine
  838.             Else
  839.                 bFoundData = True
  840.             End If
  841.         End If
  842.         
  843.         sName = CStr(splitArray(0))
  844.         x = val(splitArray(1))
  845.         y = val(splitArray(2))
  846.         z = val(splitArray(3))
  847.         
  848.         'set defaults
  849.         i = i + 1
  850.         size = 1
  851.         color = D3DCOLORVALUEtoLONG(ColorValue4(1, (10 + i Mod 20) / 30, 0.3, (10 + (i Mod 40)) / 50))
  852.         data = ""
  853.         
  854.         If cols >= 4 Then size = val(splitArray(4))
  855.         If cols >= 5 Then color = val(splitArray(5))
  856.         If cols >= 6 Then data = splitArray(6)
  857.         
  858.         AddEntry sName, x, y, z, size, color, data
  859.         
  860.         
  861. nextLine:
  862.     Loop
  863.     Set olddata = Nothing
  864.     Close fl
  865.     Exit Sub
  866. errOut:
  867.     Set m_data = olddata
  868.     MsgBox "there was an error loading " + sFile
  869.     Close fl
  870. End Sub
  871. Sub DrawAxisNameSquare(i As Long)
  872.     Dim verts(4) As D3DVERTEX
  873.     Dim w As Single
  874.     Dim h As Single
  875.     Dim mat As D3DMATERIAL8
  876.     Dim sv As Single
  877.     Dim ev As Single
  878.     w = 2:    h = 0.25
  879.         
  880.     mat.Ambient = ColorValue4(1, 1, 1, 1)
  881.     mat.diffuse = ColorValue4(1, 1, 1, 1)
  882.     sv = (m_font2height * (i) / kdy)
  883.     ev = (m_font2height * (i + 1) / kdy)
  884.     Select Case i
  885.         Case 0
  886.             g_dev.SetTransform D3DTS_WORLD, m_LabelX.GetUpdatedMatrix
  887.             
  888.         Case 1
  889.             'Y axis now part of HUD
  890.             Exit Sub
  891.         Case 2
  892.             g_dev.SetTransform D3DTS_WORLD, m_LabelZ.GetUpdatedMatrix
  893.             
  894.     End Select
  895.         
  896.     g_dev.SetTexture 0, m_Tex
  897.     g_dev.SetMaterial mat
  898.     With verts(0): .x = -w: .y = -h: .tu = 0: .tv = ev: .nz = -1: End With
  899.     With verts(1): .x = w: .y = -h: .tu = 1: .tv = ev: .nz = -1: End With
  900.     With verts(2): .x = w: .y = h: .tu = 1: .tv = sv: .nz = -1: End With
  901.     With verts(3): .x = -w: .y = h: .tu = 0: .tv = sv: .nz = -1: End With
  902.     g_dev.SetVertexShader D3DFVF_VERTEX
  903.     g_dev.DrawPrimitiveUP D3DPT_TRIANGLEFAN, 2, verts(0), Len(verts(0))
  904.     With verts(0): .z = 0.01: .x = w: .y = -h: .tu = 0: .tv = ev: .nz = 1: End With
  905.     With verts(1): .z = 0.01: .x = -w: .y = -h: .tu = 1: .tv = ev: .nz = 1: End With
  906.     With verts(2): .z = 0.01: .x = -w: .y = h: .tu = 1: .tv = sv: .nz = 1: End With
  907.     With verts(3): .z = 0.01: .x = w: .y = h: .tu = 0: .tv = sv: .nz = 1: End With
  908.     g_dev.SetVertexShader D3DFVF_VERTEX
  909.     g_dev.DrawPrimitiveUP D3DPT_TRIANGLEFAN, 2, verts(0), Len(verts(0))
  910. End Sub
  911. Sub DeleteDeviceObjects()
  912.     Set m_font = Nothing
  913.     Set m_font2 = Nothing
  914. End Sub
  915.